import Taro, { hideLoading, showLoading } from '@tarojs/taro'

const BASE_URL = 'https://tool.kenrou.cn'

export function _request(url, param, isToken = false, method = 'GET', isLoading = false){
  let header = {}
  header['content-type'] =  'application/json'
  if (isToken) {
    // 这里的Token是后台返回的存入到redux中或者localstorage中
    header['token'] = Taro.getStorageSync('_token')
  }

  if (isLoading) {
    showLoading()
  }

  return new Promise((reslove, reject) => {
    Taro.request({
      url: BASE_URL + url,
      data: param,
      method,
      header,
      success: function (res) {
        reslove(res)
      },
      fail: (err) => {
        reject(err)
      },
      complete: (complete) => {
        if (isLoading) {
          hideLoading()
        }
        reslove(complete, 'complete')
      }
    })
  })
  
}